//
// Created by LiuYou on 2021/5/12.
//

/*
 * 注意的问题:
 * pop_back() 删除最后一个元素(如果是类类型)是否调用析构函数释放资源?
 * 答案是一定调用析构函数
 *
 * erase() 删除一个元素或者一段元素(如果是类类型)是否调用析构函数释放资源?
 * 答案是一定调用析构函数
 *
 * 大小和容量
 *
 */
#include <vector>
#include <iostream>

static void testCapacityAndSize();

void test_pop_back();

void test_erase();

int main() {
//    testCapacityAndSize();

    // 这两个函数可能有问题, 所以得出来的结论也有问题。
    {
//        test_pop_back();
//        test_erase();
    }


    return 0;
}

void test_erase() {
    std::vector<int> vec{1, 2, 3, 4, 5, 6, 7};
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;
    vec.erase(vec.cbegin() + 1, vec.cend() - 1);
    for (const auto& item : vec) {
        std::cout << item << " |";
    }
    std::cout << std::endl;
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;
    std::cout << vec[2] << std::endl;
    std::cout << vec[3] << std::endl;
    std::cout << vec[4] << std::endl;
    std::cout << vec[5] << std::endl;
    std::cout << vec[6] << std::endl;
    // 运行结果:
//    /home/lucas/CLionProjects/CppStandardLibrary-Linux/cmake-build-debug/VectorTest01
//    vec.capacity(): 7
//    vec.size(): 7
//    1 |7 |
//    vec.capacity(): 7
//    vec.size(): 2
//    3
//    4
//    5
//    6
//    7
//
//    Process finished with exit code 0


}

void test_pop_back() {
    std::vector<int> vec{1, 2, 3, 4, 5, 6, 7};
    vec.push_back(8);
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;
    std::cout << vec[7] << std::endl;
    std::cout << vec[8] << std::endl;
    std::cout << vec[9] << std::endl;
    std::cout << vec[10] << std::endl;
    vec.pop_back();
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;
    std::cout << vec[6] << std::endl;
    std::cout << vec[7] << std::endl;
    std::cout << vec[8] << std::endl;
    std::cout << vec[9] << std::endl;
    std::cout << vec[10] << std::endl;
    // 运行结果
//    /home/lucas/CLionProjects/CppStandardLibrary-Linux/cmake-build-debug/VectorTest01
//    vec.capacity(): 14
//    vec.size(): 8
//    8
//    TODO 注意: 这里在size()和capacity()之间的内容, vec自动进行了默认初始化。
//    0
//    0
//    0
//    vec.capacity(): 14
//    vec.size(): 7
//    7
//    TODO 注意: 这里的vec[7]依然是8就说明, pop_back()函数仅仅是将vector内的 finish 迭代器 -1 而已。
//      并没有将 删除的 vec[7] 中的元素值为 0。
//    8
//    0
//    0
//    0
//
//    Process finished with exit code 0



}

static void testCapacityAndSize() {
    std::vector<int> vec{1, 2, 3, 4, 5, 6, 7};
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;

    vec.push_back(8);
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;

    // 运行结果
//    /home/lucas/CLionProjects/CppStandardLibrary-Linux/cmake-build-debug/VectorTest01
//    vec.capacity(): 7
//    vec.size(): 7
//    vec.capacity(): 14
//    vec.size(): 8
//
//    Process finished with exit code 0
}
